package cn.tedu.boot1.controller;

import cn.tedu.boot1.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class ProductController {
    @RequestMapping("/insert")
    @ResponseBody

    public String insert(Product product){
        System.out.println("product = " + product);
        try (Connection conn = DBUtils.getConn()){
            //获取插入数据库语句
            String sql = "insert into product values(null,?,?,?)";
            //创建执行sql语句
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换,username,等
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.executeUpdate();//执行sql语句

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "测试成功";
    }

    @RequestMapping("/select")
    @ResponseBody

    public String select(){
        //创建集合用来装商品对象
        ArrayList <Product> list = new ArrayList<>();

        try (Connection conn = DBUtils.getConn()){
            String sql = "select * from product";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            //遍历结果集中的所有信息
            while(rs.next()){
                //创建商品的对象,把参数封装到对象中
                Product p =new Product();
                p.setId(rs.getInt(1));
                p.setTitle(rs.getString(2));
                p.setPrice(rs.getDouble(3));
                p.setNum(rs.getInt(4));
                System.out.println(p);
                list.add(p);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        String html = "<table border='1'>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        //遍历集合中商品的信息 然后往表格标签里面添加tr和td
        for (Product p: list) {
            html+="<tr>";
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td>"+p.getNum()+"</td>";
            html+="<td><a href = '/delete?id="+p.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from product where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();//执行删除sql语句
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成 <a href='select'>返回列表</a>";
    }

    @RequestMapping("/update")
    @ResponseBody

    public String update(Product product){
        System.out.println("product = " + product);

        try (Connection conn = DBUtils.getConn()){
            String sql = "update product set title=?,price=?,num=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.setInt(4,product.getId());
            ps.executeUpdate();//修改执行
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "修改完成!<a href='/select'>返回列表页面</a>";
    }


}
